package advanced.physics;

import java.util.ArrayList;

import org.jbox2d.dynamics.World;
import org.mt4j.MTApplication;
import org.mt4j.components.MTComponent;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.ToolsMath;
import org.mt4j.util.math.Vector3D;

import advanced.physics.physicsShapes.PhysicsCircle;
import advanced.physics.util.PhysicsHelper;

public class RunGame implements Runnable{

	private ArrayList<PhysicsCircle>circleArray;
	private MTApplication mtApplication, app;
	private World world;
	private float scale;
	private MTComponent physicsContainer;

	public RunGame(ArrayList<PhysicsCircle> circleArray, MTApplication mtApplication, MTApplication app, World world, float scale, MTComponent physicsContainer){
		this.circleArray = circleArray;
		this.mtApplication = mtApplication;
		this.app = app;
		this.world = world;
		this.scale = scale;
		this.physicsContainer = physicsContainer;
	}

	@Override
	public void run() {
		int count = 0;
		long startTime = System.currentTimeMillis();
		long lastTime = startTime;

		while(true){

			long currentTime = System.currentTimeMillis();
			if ((currentTime - lastTime) > 500){
				PhysicsCircle c = new PhysicsCircle(app, new Vector3D(ToolsMath.getRandom(60, mtApplication.width-60), 60), 50, world, 1.0f, 0.3f, 0.4f, scale);
				MTColor col = new MTColor(ToolsMath.getRandom(60, 255),ToolsMath.getRandom(60, 255),ToolsMath.getRandom(60, 255));
				c.setFillColor(col);
				c.setStrokeColor(col);
				while(c.getBody() == null){
				}
				PhysicsHelper.addDragJoint(world, c, c.getBody().isDynamic(), scale);
				physicsContainer.addChild(c);
				circleArray.add(c);
				count++;
				lastTime = currentTime;
			}

			if ((currentTime - startTime) > 60000){
				for (PhysicsCircle circle : circleArray) {
					circle.destroy();
				}
				break;
			}


		}
	}
}
